//
//  WBWelcomeViewController.swift
//  SainMicroblog
//
//  Created by chenxinju on 16/5/6.
//  Copyright © 2016年 chenxinju-a2. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WBWelcomeViewController: UIViewController {
    
    
    //拥有ViewModel来给控件赋值
    var welcomeViewModel: WBStatusViewModel?{
    
        didSet{
//          photoImageView.image = welcomeViewModel?.
            
           // nameLabel.text = welcomeViewModel?.screen_name
        
        }
    
    }
    
    
    //加载view已经完成调用
    override func loadView() {
        view = bgImageView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    //布局子控件的方法
    override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
        
    }
    
    //正在开始加载的时候调用
    override func viewWillDisappear(animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    //view加载完成的生活调用 完成时调用动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidDisappear(animated)
         startAnmation()
        
    }
    
    //MARK:-- 动画反弹上去
    private func startAnmation() {
      self.photoImageView.snp_updateConstraints { (make) -> Void in
        make.top.equalTo(self.view.snp_top).offset(100)
        }
        
      UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { () -> Void in
        //动画执行 更新约束
        self.view.layoutIfNeeded()
        
        
        }) { (_) -> Void in
          //动画完成 切换视图控制器
///  1.步 发送一个通知
         NSNotificationCenter.defaultCenter().postNotificationName(kNotificationChangeViewControler, object: self)
    }
    }
    //添加布局的方法
    func setupUI() {
       //添加到view
       
        view.addSubview(photoImageView)
        view.addSubview(nameLabel)
        
        photoImageView.snp_remakeConstraints { (make) -> Void in
           make.centerX.equalTo(view.snp_centerX)
           make.top.equalTo(view.snp_top).offset(UIScreen.mainScreen().bounds.height-250)
        }
        
        nameLabel.snp_remakeConstraints { (make) -> Void in
         make.centerX.equalTo(view.snp_centerX)
         make.top.equalTo(photoImageView.snp_bottom).offset(10)
        }
        
    }
    
    
    //背景视图
    private  lazy var bgImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "ad_background")
        return img
    }()
    
    //头像
    private lazy var photoImageView: UIImageView = {
        let img = UIImageView()
        
//        sd_setImageWithURL(orginalViewModel?.profile_url)
       // img.sd_setImageWithURL()
       // img.image = UIImage(named: "avatar_default_big")
        
        //设置圆角
        img.layer.cornerRadius = 170/2/2
        //设置颜色
        //img.layer.shadowColor = UIColor.lightGrayColor().CGColor
        img.layer.borderColor = UIColor.lightGrayColor().CGColor
        //设置边缘线条的宽度
        img.layer.borderWidth = 2
        
        //裁剪
        img.layer.masksToBounds = true
        
        return img
    }()
    
    
   private lazy var nameLabel:UILabel = {
        let label = UILabel()
        
        label.text = "欢迎归来"
        label.textColor = UIColor.lightGrayColor()
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.numberOfLines = 0
        return label
    }()

}
